package com.ai.controller.made;


import com.ai.annotation.EnterPathParamLog;
import com.ai.common.CommonResponse;
import com.ai.entity.alarm.SteelCoilLocateTail;
import com.ai.entity.alarm.SteelCoilLocateTailBO;
import com.ai.entity.alarm.SteelCoilLocateTailPage;
import com.ai.service.made.SteelCoilLocateTailService;
import com.ai.util.Base64Util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mr.G
 * @since 2023-04-11
 */
@RestController
@RequestMapping("/steelCoilLocateTail")
public class SteelCoilLocateTailController {

    @Resource
    SteelCoilLocateTailService steelCoilLocateTailService;

    @ApiOperation("添加")
    @PostMapping("add")
    @EnterPathParamLog
    public CommonResponse add(@RequestBody SteelCoilLocateTailBO steelCoilLocateTailBO) throws ParseException {
        SteelCoilLocateTail steelCoilLocateTail = new SteelCoilLocateTail();
        BeanUtils.copyProperties(steelCoilLocateTailBO, steelCoilLocateTail);

//        SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式
        String webPhotoPath;

        String photo = steelCoilLocateTailBO.getSnapPhoto();
        if (photo.indexOf("http://") != -1) {
            webPhotoPath = photo;
        } else {
            webPhotoPath = Base64Util.GenerateFile("/images/", photo, "jpg", "D4", 2305, steelCoilLocateTailBO.getState());
        }
        steelCoilLocateTail.setCreateTime(new Date()).setSnapPhoto(webPhotoPath).setAiimg(webPhotoPath);
        steelCoilLocateTailService.save(steelCoilLocateTail);
        return CommonResponse.ok("定尾接收成功");
    }

    @ApiOperation("分页查询")
    @PostMapping("list")
    @EnterPathParamLog
    public CommonResponse list(@RequestBody SteelCoilLocateTailPage page) {
        QueryWrapper<SteelCoilLocateTail> queryWrapper = page.getQuery(new QueryWrapper<>());
        IPage<SteelCoilLocateTail> iPage = steelCoilLocateTailService.page(page, queryWrapper);
        return CommonResponse.ok(iPage);
    }
}